package com.tao.houre.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tao.common.entity.UserEntity;
import com.tao.common.utils.JwtUtil;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.R;
import com.tao.houre.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 
 *
 * @author xuwanchao
 * @email 2020381962@gmail.com
 * @date 2021-10-13 19:41:26
 */
@RestController
@RequestMapping("houre/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 登录
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity userEntity, HttpServletRequest request){
        String tokenCookie = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if (name.equals("token")){
                    tokenCookie = cookie.getValue();
                }
            }
        }
        String s = stringRedisTemplate.opsForValue().get("token:" + tokenCookie);

        if (s != null){
            //登录成功后将token值写入cookie                                                                                                                            =

            return R.ok();
        }

        UserEntity userName = userService.getOne(new QueryWrapper<UserEntity>().eq("user_name", userEntity.getUserName()));
        if (userName != null){
            if (userName.getUserPassword().equals(userEntity.getUserPassword())){
                String userNameToString = JSONObject.toJSONString(userName);
                String token = jwtUtil.createJWT(userName.getId().toString(), userName.getUserName(), "user");
                System.out.println(token);
                stringRedisTemplate.opsForValue().set("token:"+token,userNameToString,30, TimeUnit.MINUTES);
                return R.ok();
            }
            return R.error().put("data","密码错误");
        }
        return R.error().put("data","该用户不存在");
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
